package com.yswee.mallnavi.view.brand;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yswee.framework.common.cache.image.ImageWorker;
import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.framework.net.http.Entity;
import com.yswee.framework.utils.OSUtils;
import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.MToast;
import com.yswee.framework.widget.clickedtab.IClickedTabContext;
import com.yswee.framework.widget.layoutview.MRelativeLayout;
import com.yswee.framework.widget.loading.LoadingLayout;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.BrandEntity;
import com.yswee.mallnavi.entity.info.CategoryEntity;
import com.yswee.mallnavi.model.info.BrandModel;

import java.util.ArrayList;

public class BrandView extends LoadingLayout {

    private ContentView mContentView;

    private BrandModel mBrandModel;

    public BrandView(Context context) {
        super(context);
    }

    public BrandView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View getLayoutView() {
        mContentView = new ContentView(mContext);
        return mContentView;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected void onApplyData() {
        super.onApplyData();
    }

    @Override
    public void onApplyLoadingData() {
        getDataInfo();
    }

    private void getDataInfo() {
        if(mBrandModel == null) {
            ITaskContext context = mContext instanceof ITaskContext ?
                    (ITaskContext) mContext : null;
            mBrandModel = new BrandModel(context);
        }
        gotoLoading();
        mBrandModel.getCategoryList(new Callback<BaseArrayData<CategoryEntity>>() {
            @Override
            public void onFinish(Entity<BaseArrayData<CategoryEntity>> entity) {
                IParser<BaseArrayData<CategoryEntity>> parser = entity.getParser();
                final BaseArrayData<CategoryEntity> data = entity.getParsedData();
                if(parser.getIsOk() && data != null && data.getArray() != null) {
                    applyDataInfo(data.getArray());
                    gotoSuccessful();
                    return;
                }
                String msg = entity.getMessage();
                if(StringUtils.isEmpty(msg)) {
                    msg = getResources().getString(R.string.brand_getdata_fail);
                }
                MToast.showToastMessage(msg);
                gotoError();
            }

            @Override
            public void onError(Entity<BaseArrayData<CategoryEntity>> entity) {
                gotoError();
                if(entity.getEntityStatus() != Entity.NETINVALID) {
                    MToast.showToastMessage(R.string.brand_getdata_fail);
                }
            }
        });
    }

    private void applyDataInfo(ArrayList<CategoryEntity> array) {
        CategoryEntity categoryAll = new CategoryEntity();
        categoryAll.id = "";
        categoryAll.name = mContext.getResources().getString(R.string.all);
        categoryAll.iconUrl = ImageWorker.RESFILE_PREFIX + R.drawable.category_all;
        array.add(0, categoryAll);
        mContentView.setDataSource(array);
    }

    private class ContentView extends MRelativeLayout<ArrayList<CategoryEntity>> {

        @ViewInject
        private LinearLayout lltitle;
        @ViewInject
        private View rlcondition;
        @ViewInject
        private EditText tvkeyword;
        @ViewInject
        private Button btnsearch;
        @ViewInject
        private IGridView grddata;
        @ViewInject
        private LetterView uvletter;
        @ViewInject
        private TabBar uvtabbar;
        @ViewInject
        private InfoView uvinfo;

        private IGridView.IActionCallback mActionCallbackProxy = new IGridView.IActionCallback() {
            @Override
            public void onBrandClicked(View v, BrandEntity brand) {
                lltitle.setVisibility(View.GONE);
                rlcondition.setVisibility(View.GONE);
                grddata.setVisibility(View.GONE);
                uvletter.setVisibility(View.GONE);
                uvtabbar.setVisibility(View.GONE);
                uvinfo.setVisibility(View.VISIBLE);
                uvinfo.setDataSource(brand);
            }
        };

        public ContentView(Context context) {
            super(context);
        }

        public ContentView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        protected int getLayoutResId() {
            return R.layout.view_info_brand;
        }

        @Override
        protected void onBindListener() {
            super.onBindListener();
            grddata.setActionCallback(mActionCallbackProxy);
            uvletter.setOnTouchingLetterChangedListener(
                    new LetterView.OnTouchingLetterChangedListener() {
                        @Override
                        public void onTouchingLetterChanged(String s, float y, float x) {
                            grddata.setSelectionKey(s);
                        }
                    });
            tvkeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    switch (actionId) {
                        case EditorInfo.IME_ACTION_SEARCH:
                            applySearch();
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });
            btnsearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    applySearch();
                }
            });
            uvtabbar.setListener(new IClickedTabContext() {
                @Override
                public void onTabChanged(int index) {
                    grddata.setParams(mDataItem.get(index).id);
                    applySearch();
                }
            });
            uvinfo.setActionCallback(new InfoView.IActionCallback() {
                @Override
                public void onBackClicked(View v) {
                    uvinfo.setVisibility(View.GONE);
                    lltitle.setVisibility(View.VISIBLE);
                    rlcondition.setVisibility(View.VISIBLE);
                    grddata.setVisibility(View.VISIBLE);
                    uvletter.setVisibility(View.VISIBLE);
                    uvtabbar.setVisibility(View.VISIBLE);
                }
            });
        }

        @Override
        protected void onApplyData() {
            uvtabbar.setDataArray(mDataItem);
            grddata.setParams("");
            applySearch();
        }

        private void applySearch() {
            OSUtils.hideSoftInput(mContext);
            String keyword = tvkeyword.getText().toString().trim();
            grddata.setFilter(keyword);
            grddata.refresh();
        }
    }
}
